package controller

import (
	"errors"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
	"gitee.com/fierce_wolf/go-fox-edge-common/commRedisService"
	"gitee.com/fierce_wolf/go-fox-edge-common/commSql"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Pair"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/String"
	"gitee.com/fierce_wolf/go-fox-edge-common/edgeEntity"
	"gitee.com/fierce_wolf/go-fox-edge-common/httpserver"
	"go-fox-edge-kernel-manager/utils/ExtendUtils"
	"go-fox-edge-kernel-manager/utils/PageUtils"
)

type deviceHistoryManageController struct {
}

func (e *deviceHistoryManageController) init() {
	httpserver.Server.POST("/device/history/page", e.selectEntityPage)
}

func (e *deviceHistoryManageController) selectEntityPage(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	// 提取业务参数
	deviceName := Map.GetString(body, "deviceName", "")
	objectName := Map.GetString(body, "objectName", "")
	pageNum := Map.GetInt(body, "pageNum", 1)
	pageSize := Map.GetInt(body, "pageSize", 10)

	// 简单校验参数
	if Map.HasNull(body, "pageNum", "pageSize") {
		return nil, errors.New("参数不能为空:pageNum, pageSize")
	}

	sb := ""
	if deviceName != "" {
		deviceEntity := commRedisService.GetDeviceEntity(deviceName)
		if deviceEntity != nil {
			sb += " (device_id = " + String.ToString(deviceEntity.BaseEntity.Id) + ") AND"
		}
	}
	if objectName != "" {
		sb += " (object_name = '" + objectName + "') AND"
	}
	if sb != "" {
		sb = sb[0 : len(sb)-len("AND")]
	}

	total, entityList, err := e.selectEntityListPage(sb, "DESC", pageNum, pageSize)
	if err != nil {
		return nil, err
	}

	// 添加扩展信息
	ids := make(map[int64]interface{})
	for _, entity := range entityList {
		historyEntity := entity.(*edgeEntity.DeviceHistoryEntity)
		ids[historyEntity.DeviceId] = nil
	}

	deviceEntityList := commRedisService.FindEntityList(edgeEntity.Type.DeviceEntity.Type(), func(entity commEntity.IEntity) bool {
		deviceEntity := entity.(*edgeEntity.DeviceEntity)

		_, ok := ids[deviceEntity.BaseEntity.Id]
		return ok
	})

	mapList := commEntity.BuildMapList(entityList)

	keypairs := make([]*Pair.Pair, 0)
	keypairs = append(keypairs, &Pair.Pair{Key: "deviceId", Value: "id"})
	extend := make([]string, 0)
	extend = append(extend, "deviceName")
	extend = append(extend, "manufacturer")
	extend = append(extend, "deviceType")
	extend = append(extend, "channelName")
	extend = append(extend, "channelType")

	ExtendUtils.Extend(mapList, commEntity.BuildMapList(deviceEntityList), keypairs, extend)

	data := make(map[string]interface{})
	data["list"] = mapList
	data["total"] = total

	return data, nil
}

func (e *deviceHistoryManageController) selectEntityListPage(filter string, order string, pageNum int, pageSize int) (int, []commEntity.IEntity, error) {
	// 从数据库的deviceObject中查询总数
	selectCount := PageUtils.MakeSelectCountSQL("tb_device_history", filter)

	total, err := commSql.QueryCount(selectCount)
	if err != nil {
		return 0, nil, err
	}

	// 分页查询数据
	selectPage := PageUtils.MakeSelectSQLPage("tb_device_history", nil, nil, filter, order, pageNum, pageSize)
	entityList, err := commSql.Client.Query(edgeEntity.Type.DeviceHistoryEntity.Type(), selectPage)
	if err != nil {
		return 0, nil, err
	}

	return total, entityList, err

}
